package
{
	import away3d.core.material.BitmapMaterial;
	import away3d.core.math.Number3D;
	import away3d.core.mesh.Segment;
	import away3d.core.mesh.Vertex;
	import away3d.core.scene.MouseEvent3D;
	import away3d.core.scene.View3D;
	import away3d.core.utils.Init;
	import away3d.extrusions.SegmentsExtrude;
	import away3d.objects.Cube;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class CubeDrag extends Sprite
	{
		private var view:View3D = new View3D({x:200, y:100});
		private var cube:Cube;
		var cube2:Cube;
		private var down:Boolean = false;
		var box:SegmentsExtrude;
		
		public function CubeDrag()
		{
			addChild(view); 
			
			var aPoints:Array = new Array();
            aPoints.push(new Number3D(-500, 0, -300));
            aPoints.push(new Number3D(500, 0, -300));
            aPoints.push(new Number3D(500, 0, 300));
            aPoints.push(new Number3D(-500, 0, 300)); 
            
            
           var seg1 = new Segment(new Vertex(0,0,0), new Vertex(-500, 0,0));
           var seg2 = new Segment(new Vertex(0,0,0), new Vertex(500, 0,0));  
            var mat:BitmapMaterial  = new BitmapMaterial(new BitmapData(256,256, false, 0x006699), {smooth:false, debug:true}); 
			box = new SegmentsExtrude(aPoints,{material:mat, coverall:false,  
axis:"y", offset:300, closepath:true, subdivision:1,  
thickness_subdivision:1, flip:false,  thickness:50, bothsides:true,  
recenter:true, omit:"bottom"});
box.moveBackward(2000); 

			view.scene.addOnMouseDown(press); 
			view.scene.addOnMouseUp(up);
			view.scene.addOnMouseOut(up);  
			
			cube = new Cube();
			
			cube2= new Cube({size:100,width:100,height:100,depth:100});
			
			cube2.x = 101;
			view.scene.addChild(box);
	        view.scene.addChild(cube);
	        view.scene.addChild(cube2);
//	        view.camera.x=view.camera.y=view.camera.z=200; 

	        
	        
			addEventListener(Event.ENTER_FRAME,go);
		}

		var dx:Number;
		var dy:Number;
		public function press(e:MouseEvent3D):void {
			dx = cube.x - e.sceneX;
			dy = cube.y - e.sceneY;
			view.scene.addOnMouseMove(mv); 
			down = true;
		}
		public function go(event:Event):void{
//			box.yaw(2);
//			box.roll(2);

			view.render(); 
		}
		public function mv(e:MouseEvent3D):void{
			if(down){
				var lx =  ((e.sceneX + dx) - cube.x );
				var ly =  (e.sceneY + dy) - cube.y ;
				cube.x  += lx;
				cube.y  += ly;
				
					
				if(cube.distanceTo(cube2)<= (100)){
					cube.x -= lx;
					cube.y -= ly;
				}
			}
		}
		public function up(e:MouseEvent3D):void{
			view.scene.removeOnMouseMove(mv); 
			down = false;
			
		}
	}
}